package com.houdoudou.bean;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;

import com.houdoudou.common.CommonMathFunction;
import com.houdoudou.dao.ConnFactory;
import com.houdoudou.dao.OwnerDao;
public class Supplier {

	
	String telnum="";
	String secret="";	
	String name="";
	String address="";
	String owner="";
	String serviceTelnum="";
	int isAuth=-1;
	int minuteMini=-1;   //提前下单
	double moneyMini=-1;  // 最小起订
	double rate=-1 ; //公司和业主商定的提成比例，该比例属于公司
	double boxFeeSu=-1;  //业主向商家收取的箱格使用费
    double boxFeeCu=-1;  // 商家向消费者收取的箱格使用费
    String major=""; //主营
	public Supplier() {
	
	}
	public String getTelnum() {
		return telnum;
	}
	public void setTelnum(String telnum) {
		this.telnum = telnum;
	}
	public String getSecret() {
		return secret;
	}
	public void setSecret(String secret) {
		this.secret = secret;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public String getServiceTelnum() {
		return serviceTelnum;
	}
	public void setServiceTelnum(String serviceTelnum) {
		this.serviceTelnum = serviceTelnum;
	}
	public int getIsAuth() {
		return isAuth;
	}
	public void setIsAuth(int isAuth) {
		this.isAuth = isAuth;
	}
	public int getMinuteMini() {
		return minuteMini;
	}
	public void setMinuteMini(int minuteMini) {
		this.minuteMini = minuteMini;
	}
	public double getMoneyMini() {
		return moneyMini;
	}
	public void setMoneyMini(double moneyMini) {
		this.moneyMini = moneyMini;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}
	public double getBoxFeeSu() {
		return boxFeeSu;
	}
	public void setBoxFeeSu(double boxFeeSu) {
		this.boxFeeSu = boxFeeSu;
	}
	public double getBoxFeeCu() {
		return boxFeeCu;
	}
	public void setBoxFeeCu(double boxFeeCu) {
		this.boxFeeCu = boxFeeCu;
	}
		 
    public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
public static Supplier beanFromRequest(HttpServletRequest req) throws Exception {
	
		Supplier supplier= new Supplier();
		if(req.getParameter("telnum")!=null){
			supplier.setTelnum(req.getParameter("telnum"));
		}
		if(req.getParameter("secret")!=null){
			supplier.setSecret(req.getParameter("secret"));
		}
		if(req.getParameter("name")!=null){
			supplier.setName(req.getParameter("name"));
		}
		if(req.getParameter("address")!=null){
			supplier.setAddress(req.getParameter("address"));
		}
		if(req.getParameter("owner")!=null){
			supplier.setOwner(req.getParameter("owner"));
		}
		if(req.getParameter("serviceTelnum")!=null){
			supplier.setServiceTelnum(req.getParameter("serviceTelnum"));
		}
		
		if(req.getParameter("isAuth")!=null){
			if(!req.getParameter("isAuth").isEmpty()){
			int isAuth=Integer.parseInt(req.getParameter("isAuth"));
			supplier.setIsAuth(isAuth);
		}}
		if(req.getParameter("minuteMini")!=null){
			if(!req.getParameter("minuteMini").isEmpty()){
			int minuteMini=Integer.parseInt(req.getParameter("minuteMini"));
			supplier.setMinuteMini(minuteMini);
		}}
		if(req.getParameter("moneyMini")!=null){
			if(!req.getParameter("moneyMini").isEmpty()){
			double moneyMini=Double.parseDouble(req.getParameter("moneyMini"));
			supplier.setMoneyMini(moneyMini);
		}}
		if(req.getParameter("rate")!=null){
			if(!req.getParameter("rate").isEmpty()){
			double rate=Double.parseDouble(req.getParameter("rate"));
			supplier.setRate(rate);
		}}
		if(req.getParameter("boxFeeSu")!=null){
			if(!req.getParameter("boxFeeSu").isEmpty()){
			double boxFeeSu=Double.parseDouble(req.getParameter("boxFeeSu"));
			supplier.setBoxFeeSu(boxFeeSu);
		}}
		if(req.getParameter("boxFeeCu")!=null){
			if(!req.getParameter("boxFeeCu").isEmpty()){
			double boxFeeCu=Double.parseDouble(req.getParameter("boxFeeCu"));
			supplier.setBoxFeeCu(boxFeeCu);
		}}	
		if(req.getParameter("major")!=null){
			supplier.setServiceTelnum(req.getParameter("major"));
		}
		print("bean from request  ",supplier);
        return  supplier;
     }
public static  void print(String when,Supplier supplier){
	System.out.println(when
			+ " telnum ="+ supplier.getTelnum()
	   		+ " secret ="+supplier.getSecret()		
			+ " name ="+supplier.getName()
	   		+ " address ="+supplier.getAddress()
	   		+ " owner ="+ supplier.getOwner()
	   		+ " serviceTelnum ="+supplier.getServiceTelnum()	   		
	   		+ " isAuth = " +supplier.getIsAuth()
	   		+ " minuteMini =" +supplier.getMinuteMini()
	   		+ " moneyMini =" +supplier.getMoneyMini()
	   		+ " rate ="+supplier.getRate()  
	   		+ " boxFeeSu ="+supplier.getBoxFeeSu()  
	   		+ " boxFeeCu ="+ supplier.getBoxFeeCu()
	   		+ " major ="+ supplier.getMajor()
	   		);
}

public static Supplier  merge(Supplier supplierExist,Supplier supplierGiven ){
	if(!supplierGiven.getTelnum().isEmpty()){
		supplierExist.setTelnum(supplierGiven.getTelnum());
	}
	if(!supplierGiven.getSecret().isEmpty()){
		supplierExist.setSecret(supplierGiven.getSecret());
	}
	if(!supplierGiven.getName().isEmpty()){
		supplierExist.setName(supplierGiven.getName());
	}
	if(!supplierGiven.getAddress().isEmpty()){
		supplierExist.setAddress(supplierGiven.getAddress());
	}
	if(!supplierGiven.getOwner().isEmpty()){
		supplierExist.setOwner(supplierGiven.getOwner());
	}
	if(!supplierGiven.getServiceTelnum().isEmpty()){
		supplierExist.setServiceTelnum(supplierGiven.getServiceTelnum());
	}
	if(supplierGiven.getIsAuth()!=-1){
		supplierExist.setIsAuth(supplierGiven.getIsAuth());
	}
	if(supplierGiven.getMinuteMini()!=-1){
		supplierExist.setMinuteMini(supplierGiven.getMinuteMini());
	}
	if(supplierGiven.getMoneyMini()!=-1){
		supplierExist.setMoneyMini(supplierGiven.getMoneyMini());
	}
	if(supplierGiven.getRate()!=-1){
		supplierExist.setRate(supplierGiven.getRate());
	}
	if(supplierGiven.getBoxFeeCu()!=-1){
		supplierExist.setBoxFeeCu(supplierGiven.getBoxFeeCu());
	}
	if(supplierGiven.getBoxFeeSu()!=-1){
		supplierExist.setBoxFeeSu(supplierGiven.getBoxFeeSu());
	}
	print("bean after merg  ", supplierExist);
	return supplierExist;
}   	
   public static void checkValid(Supplier supplier) throws Exception{
	   if(supplier.getTelnum().length()!=11){  throw new BeanParseException("电话位数不对");}
	   if(!CommonMathFunction.isNumeric(supplier.getTelnum())){ throw new BeanParseException("逗我玩儿吗？电话必须是数字，好吗？"); }
		if(supplier.getSecret().isEmpty()){ throw new BeanParseException("密码为空");}
		if(supplier.getName().isEmpty()){ throw new BeanParseException("商家名称为空");}
		if(supplier.getAddress().isEmpty()){ throw new BeanParseException("地址为空");}
		if(supplier.getOwner().length()!=11){ throw new BeanParseException("业主不对");}
		 if(!CommonMathFunction.isNumeric(supplier.getOwner())){ throw new BeanParseException("逗我玩儿吗？业主电话必须是数字，好吗？"); }
		if(supplier.getServiceTelnum().isEmpty()){ throw new BeanParseException("服务电话位数不对");}
		if(supplier.getServiceTelnum().length()!=11){ throw new BeanParseException("服务电话位数不对");}
		if(!CommonMathFunction.isNumeric(supplier.getServiceTelnum())){ throw new BeanParseException("逗我玩儿吗？服务电话必须是数字，好吗？"); }
		if((supplier.getIsAuth()!=1)&&(supplier.getIsAuth()!=2)){throw new BeanParseException("未设置是否上线");}
		if(supplier.getMinuteMini()<0||supplier.getMinuteMini()>4320){throw new BeanParseException("提前下单时间不能超过三天");}
		if(supplier.getMoneyMini()<0 ||supplier.getMoneyMini()>30 ){throw new BeanParseException("最小起订在0到30元之间");	}
		if(supplier.getRate()<0||supplier.getRate()>1){throw new BeanParseException("提成比例在0到1之间");	}
		if(supplier.getBoxFeeCu()<0||supplier.getBoxFeeCu()>10){throw new BeanParseException("商家收取的箱格使用费在0到10之间"); }
		if(supplier.getBoxFeeSu()<0||supplier.getBoxFeeSu()>10){throw new BeanParseException("业主收取的箱格使用费在0到10之间"); }		
		Connection conn= ConnFactory.getConn();	 
		   OwnerDao ownerDao= new  OwnerDao(conn);
		   Owner ownerExist= ownerDao.selectByTelnum(supplier.getOwner());
		   ConnFactory.closeConn(conn);
		if((ownerExist.getRate()+supplier.getRate())>1){throw new BeanParseException("业主对商家的提成与公司对业主的提成之和不能超过1");	}   

      }
   
	public static  String  getSearchSql(Supplier supplier){
		String sql="select * from supplier";
		int count=0;
		if(!supplier.getTelnum().isEmpty()){ sql="select * from supplier where telnum = "+supplier.getTelnum()+""; return sql;
		 }else{
			
			if(!supplier.getOwner().isEmpty()) {	
				if(count==0) {sql= sql+" where owner = "+supplier.getOwner();
				}else{
					sql= sql+" and  owner = "+supplier.getOwner();
				  }  
				  count++; }
			
			if(supplier.getIsAuth()!=-1) {	
				if(count==0) {sql= sql+" where isAuth = "+supplier.getIsAuth();
				}else{
					sql= sql+" and  isAuth = "+supplier.getIsAuth();
				  }  
				  count++; }
			
		}
	  System.out.println(" supplier search sql="+sql);
		return sql;
	}
  
   public  static Supplier encrypt(Supplier supplier) throws Exception{
		String cipertext= PasswordUtil.encrypt(supplier.getSecret());
		supplier.setSecret(cipertext);	   
	   return supplier;
   }
   public  static Supplier decrypt(Supplier supplier) throws Exception{
 		String plaintext= PasswordUtil.decrypt(supplier.getSecret());
 		supplier.setSecret(plaintext);	   
 	   return supplier;
    }
   public  static Supplier transEncrypt(Supplier supplier) throws Exception{
		String cipertext= TransPasswordUtil.encrypt(supplier.getSecret());
		supplier.setSecret(cipertext);	   
	   return supplier;
  }
  public  static Supplier transDecrypt(Supplier supplier) throws Exception{
		String plaintext= TransPasswordUtil.decrypt(supplier.getSecret());
		supplier.setSecret(plaintext);	   
	   return supplier;
   }
}
